﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using BLLEP;
using System.IO;
using System.Drawing;

namespace EstacionDePoder.Codigo
{
    public class FormGenerico: Page
    {
        #region AppConfig

        public bool MPTest 
        {
            get
            {
                bool test = true;

                if (ConfigurationManager.AppSettings["MPTest"] != null)
                    test = ConfigurationManager.AppSettings["MPTest"].ToString().ToLower().Equals("true");

                return test;
            }
        }

        public string MPID
        {
            get
            {
                if (ConfigurationManager.AppSettings["Makalengo"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["Makalengo"].ToString();
            }
        }

        public string MPKey
        {
            get
            {
                if (ConfigurationManager.AppSettings["Kuriparta"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["Kuriparta"].ToString();
            }
        }

        public string MercadoPagoDescripcionDefecto
        {
            get
            {
                if (ConfigurationManager.AppSettings["MercadoPagoDescripcionDefecto"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["MercadoPagoDescripcionDefecto"].ToString();
            }
        }

        public decimal MercadoPagoPorcentajeComision
        {
            get
            {
                return decimal.Parse(ConfigurationManager.AppSettings["MercadoPagoPorcentajeComision"].ToString());
            }
        }

        public string CBU
        {
            get
            {
                if (ConfigurationManager.AppSettings["CBU"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["CBU"].ToString();
            }
        }

        public string NroCuenta
        {
            get
            {
                if (ConfigurationManager.AppSettings["NroCuenta"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["NroCuenta"].ToString();
            }
        }

        public string Titular
        {
            get
            {
                if (ConfigurationManager.AppSettings["Titular"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["Titular"].ToString();
            }
        }

        public string MailVentas
        {
            get
            {
                if (ConfigurationManager.AppSettings["MailVentas"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["MailVentas"].ToString();
            }
        }

        public string CUIL
        {
            get
            {
                if (ConfigurationManager.AppSettings["CUIL"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["CUIL"].ToString();
            }
        }

        public string DNI
        {
            get
            {
                if (ConfigurationManager.AppSettings["DNI"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["DNI"].ToString();
            }
        }

        public decimal DolarPeso 
        {
            get
            {
                return decimal.Parse(ConfigurationManager.AppSettings["DolarPeso"].ToString());
            }
        }

        public decimal RecargoImpositivo
        {
            get
            {
                return decimal.Parse(ConfigurationManager.AppSettings["RecargoImpositivo"].ToString());
            }
        }

        public string FormatosImagenesValidos
        {
            get
            {
                if (ConfigurationManager.AppSettings["FormatosImagenesValidos"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["FormatosImagenesValidos"].ToString();
            }
        }

        public string CarpetaComprobantes
        {
            get
            {
                if (ConfigurationManager.AppSettings["CarpetaComprobantes"] == null)
                    return null;
                else
                    return ConfigurationManager.AppSettings["CarpetaComprobantes"].ToString();
            }
        }

        #endregion

        #region Session Global

        public string EmailRegistrandose 
        {
            get
            {
                if (Session["EmailRegistrandose"] == null)
                    return null;
                else
                    return Session["EmailRegistrandose"].ToString();
            }
            set { Session["EmailRegistrandose"] = value; }
        }

        public string EmailRecuperandose
        {
            get
            {
                if (Session["EmailRecuperandose"] == null)
                    return null;
                else
                    return Session["EmailRecuperandose"].ToString();
            }
            set { Session["EmailRecuperandose"] = value; }
        }

        public string EmailActual
        {
            get
            {
                if (Session["EmailActual"] == null)
                    return null;
                else
                    return Session["EmailActual"].ToString();
            }
            set { Session["EmailActual"] = value; }
        }

        #endregion


        public static void SetearVisibilidad(WebControl control, bool visible)
        {
            if (visible)
                control.Style[@"display"] = @"";
            else
                control.Style[@"display"] = @"none";
        }

        public static void SetearVisibilidad(List<WebControl> controles, bool visible)
        {
            for (int i = 0; i < controles.Count; i++)
                SetearVisibilidad(controles[i], visible);
        }

        protected void CalcularTotal(Label lbl, bool incluirTexto)
        {
            CarritoManager manager = new CarritoManager(Context);
            List<CarritoItem> items = manager.TraerItems();
            decimal total = items.Select(i => i.Cantidad * i.Precio).Sum();

            if (items.Count == 0)
                lbl.Text = string.Empty;
            else
            {
                if(incluirTexto)
                    lbl.Text = string.Format("Total: ${0} ARS", total.ToString());
                else
                    lbl.Text = string.Format("${0} ARS", total.ToString());
            }
        }

        protected void CalcularTotal(Label lbl)
        {
            CalcularTotal(lbl, true);
        }

        protected decimal CalcularTotal(EP_Pedido pedido)
        {
            decimal total = 0;
            foreach (EP_DetallePedido item in pedido.EP_DetallePedidos)
                total += (decimal)(item.Cantidad * item.PrecioUnitario);

            return total;
        }

        protected System.Drawing.Image ConvertirEnImage(byte[] bytes)
        {
            MemoryStream ms = new MemoryStream(bytes);
            Bitmap bm = new Bitmap(ms);

            return bm;
        }
    }
}